//
//  MakeAppointmentViewController.m
//  RCA24
//
//  Created by Raul Pacurar on 2/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MakeAppointmentViewController.h"

// testing stuff.

@implementation MakeAppointmentViewController

@synthesize blackScreenView;

@synthesize tipPersoanaButton;
@synthesize dateUtilizatorButton;
@synthesize dateVehiculButton;
@synthesize dataIntrariiInVigoareButton;
@synthesize scopulUtilizariiButton;
@synthesize ateleButton;
@synthesize oferteButton;

@synthesize tipPersoanaValidationImage;
@synthesize dateUtilizatorValidationImage;
@synthesize dateVehiculValidationImage;
@synthesize dataIntrariiInVigoareValidationImage;
@synthesize scopulUtilizariiValidationImage;
@synthesize alteleValidationImage;

@synthesize tipPersoanaSupoptionsView;
@synthesize dateUtilizatorSupoptionsView;
@synthesize dateVehiculSupoptionsView;
@synthesize dataIntrariiInVigoareSupoptionsView;
@synthesize scopulUtilizariiSupoptionsView;
@synthesize alteleSupoptionsView;
@synthesize oferteView;

@synthesize tipPersoanaFirstButton;
@synthesize tipPersoanaSecondButton;
@synthesize tipPersoanaThirdButton;
@synthesize tipPersoanaFourthButton;
@synthesize tipPersoanaFifthButton;
@synthesize tipPersoanaSixthButton;
@synthesize tipPersoanaBackButton;

@synthesize judetTextField;
@synthesize localitateTextField;
@synthesize cnpTextField;
@synthesize dateUtilizatorBackButton;

@synthesize dateVehiculBackButton;

@synthesize dataIntrariiInVigoareBackButton;

@synthesize scopulUtilizariiFirstButton;;
@synthesize scopulUtilizariiSecondButton;
@synthesize scopulUtilizariiThirdButton;
@synthesize scopulUtilizariiFourthButton;
@synthesize scopulUtilizariiBackButton;

@synthesize alteleFirstButton;
@synthesize alteleSecondButton;
@synthesize alteleThirdButton;
@synthesize alteleBackButton;

@synthesize oferteTableView;
@synthesize oferteGoNextButton;
@synthesize oferteBackButton;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Appointment", @"Appointment");
        self.tabBarItem.image = [UIImage imageNamed:@"appointment"];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    NSDate *now = [[NSDate alloc] init];
    [datePicker setDate:now animated:YES];
    [now release];
    
  //  [tipPersoanaButton useBlackStyle];
    
    [super viewDidLoad];
    [self initializeScreen];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

// Destructor

- (void) dealloc
{
    [super dealloc];
    
     [blackScreenView release];
    
     [tipPersoanaButton release];
     [dateUtilizatorButton release];
     [dateVehiculButton release];
     [dataIntrariiInVigoareButton release];
     [scopulUtilizariiButton release];
     [ateleButton release];
     [oferteButton release];
    
     [tipPersoanaValidationImage release];
     [dateUtilizatorValidationImage release];
     [dateVehiculValidationImage release];
     [dataIntrariiInVigoareValidationImage release];
     [scopulUtilizariiValidationImage release];
     [alteleValidationImage release];
    
     [tipPersoanaSupoptionsView release];
     [dateUtilizatorSupoptionsView release];
     [dateVehiculSupoptionsView release];
     [dataIntrariiInVigoareSupoptionsView release];
     [scopulUtilizariiSupoptionsView release];
     [alteleSupoptionsView release];
     [oferteView release];
    
     [tipPersoanaFirstButton release];
     [tipPersoanaSecondButton release];
     [tipPersoanaThirdButton release];
     [tipPersoanaFourthButton release];
     [tipPersoanaFifthButton release];
     [tipPersoanaSixthButton release];
     [tipPersoanaBackButton release];
    
     [judetTextField release];
     [localitateTextField release];
     [cnpTextField release];
     [dateUtilizatorBackButton release];
    
     [dateVehiculBackButton release];
    
     [dataIntrariiInVigoareBackButton release];
    
     [scopulUtilizariiFirstButton release];
     [scopulUtilizariiSecondButton release];
     [scopulUtilizariiThirdButton release];
     [scopulUtilizariiFourthButton release];
     [scopulUtilizariiBackButton release];
    
     [alteleFirstButton release];
     [alteleSecondButton release];
     [alteleThirdButton release];
     [alteleBackButton release];
    
     [oferteTableView release];
     [oferteGoNextButton release];
     [oferteBackButton release];

    
}

- (void) initializeScreen
{
    [blackScreenView setAlpha:0];
    
    UIImage * backgroundPattern = [UIImage imageNamed:@"bg-app.png"];
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:backgroundPattern]];

}

- (void) openTipPersoanaSubview
{
    
}

- (void) showSuboptionsViewAnimation: (UIView*) view
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    
    [blackScreenView setAlpha:0.7];
    view.transform = CGAffineTransformMakeTranslation(-320, 0);
    
    [UIView commitAnimations];
}


- (void) hideSuboptionsViewAnimation: (UIView*) view
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    
    [blackScreenView setAlpha:0];
    view.transform = CGAffineTransformMakeTranslation(-0, 0);
    
    [UIView commitAnimations];
}

- (void) generateOffers
{
    
}



- (void) goToNextView
{
    DateAsiguratViewController *dateAsiguratViewController = [[DateAsiguratViewController alloc] init];
    [self.navigationController pushViewController:dateAsiguratViewController animated:true];
    [dateAsiguratViewController release];
}

// Callbacks

- (IBAction) openSubMenu: (UIButton*) button
{
    if (button == tipPersoanaButton)
    {
        [self openTipPersoanaSubview];
        [self showSuboptionsViewAnimation: tipPersoanaSupoptionsView];
    }
    
    else if (button == dateUtilizatorButton)
    {
        [self showSuboptionsViewAnimation: dateUtilizatorSupoptionsView];
    }
    
    else if (button == dateVehiculButton)
    {
        [self showSuboptionsViewAnimation: dateVehiculSupoptionsView];
    }
    
    else if (button == dataIntrariiInVigoareButton)
    {
        [self showSuboptionsViewAnimation: dataIntrariiInVigoareSupoptionsView];
    }
    
    else if (button == scopulUtilizariiButton)
    {
        [self showSuboptionsViewAnimation: scopulUtilizariiSupoptionsView];
    }
    
    else if (button == ateleButton)
    {
        [self showSuboptionsViewAnimation: alteleSupoptionsView];
    }
    
    else if (button == oferteButton)
    {
        [self showSuboptionsViewAnimation: oferteView];
    }

}

- (IBAction) closeSubMenu: (UIButton*) button
{
    if (button == dateUtilizatorBackButton)
    {
        [self hideSuboptionsViewAnimation:dateUtilizatorSupoptionsView];
        [dateUtilizatorValidationImage setImage:[UIImage imageNamed:@"okay.png"]];
    }
    
    else if (button == dateVehiculBackButton)
    {
        [self hideSuboptionsViewAnimation:dateVehiculSupoptionsView];
        [dateVehiculValidationImage setImage:[UIImage imageNamed:@"okay.png"]];
    }

    else if (button == dataIntrariiInVigoareBackButton)
    {
        [self hideSuboptionsViewAnimation:dataIntrariiInVigoareSupoptionsView];
        [dataIntrariiInVigoareValidationImage setImage:[UIImage imageNamed:@"okay.png"]];
    }
    
    else if (button == scopulUtilizariiBackButton)
    {
        [self hideSuboptionsViewAnimation:scopulUtilizariiSupoptionsView];
    }
    
    else if (button == alteleBackButton)
    {
        [self hideSuboptionsViewAnimation:alteleSupoptionsView];
    }
    
    else if (button == oferteBackButton)
    {
        [self hideSuboptionsViewAnimation: oferteView];
    }
}

- (IBAction) choosePersonType: (UIButton*) button
{
    if (button == tipPersoanaBackButton)
    {
        [tipPersoanaValidationImage setImage:[UIImage imageNamed:@"notOkay.png"]];
    }
    else 
    {
        [tipPersoanaValidationImage setImage:[UIImage imageNamed:@"okay.png"]];
    }
    
    [self hideSuboptionsViewAnimation:tipPersoanaSupoptionsView];
}

- (IBAction) chooseScopulUtilizarii: (UIButton*) button
{
    if (button == scopulUtilizariiBackButton)
    {
        [scopulUtilizariiValidationImage setImage:[UIImage imageNamed:@"notOkay.png"]];
    }
    else
    {
        [scopulUtilizariiValidationImage setImage:[UIImage imageNamed:@"okay.png"]];
    }
    
    [self hideSuboptionsViewAnimation:scopulUtilizariiSupoptionsView];
}

- (IBAction) chooseAltele: (UIButton*) button
{
    if (button == alteleBackButton)
    {
        [alteleValidationImage setImage:[UIImage imageNamed:@"notOkay.png"]];
    }
    else
    {
        [alteleValidationImage setImage:[UIImage imageNamed:@"okay.png"]];
    }
    
    [self hideSuboptionsViewAnimation:alteleSupoptionsView];
}

-(IBAction)textFieldReturn:(id)sender
{
    [sender resignFirstResponder];
} 

#pragma mark Table

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    OferteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"OferteTableViewCell" owner:nil options:nil];
        
        for (UIView *view in views) {
            if([view isKindOfClass:[UITableViewCell class]])
            {
                cell = (OferteTableViewCell*)view;
            }
        }
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    [self goToNextView];
}


@end
